package com.tieto.springboot.demo.customsession;
import java.util.HashMap;

import javax.annotation.PreDestroy;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
public final class JedisPoolUtil {

	private static final JedisPool pool = new JedisPool();
	
	private JedisPoolUtil(){}

	public static final Jedis getJedis() {
		Jedis jedis = pool.getResource();
		jedis.auth("tpy1209q");
		return jedis;
	}

	@PreDestroy
	private void closePool() {
		System.out.println("do close JedisPool..");
		pool.close();
	}

	public static final Object convertTo(String content, Class<?> clazz) {
		try{			
			return JSON.parseObject(content, clazz);
		}
		catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
				
	}
	
	public static final Object conver2MapOrString(String content) {
		try {
			return JSON.parseObject(content, HashMap.class);
		} catch (JSONException e) {
		}
		return JSON.parseObject(content, String.class);
	}

	public static final String toJson(Object obj) {
		return JSON.toJSONString(obj);
	}
}
